<?php
declare(strict_types=1);

namespace app\controller\merchant;

use app\BaseController;
use app\model\MerchantPaymentConfig;
use think\facade\Log;
use think\Request;

class PaymentConfigController extends BaseController
{
    /**
     * 获取支付配置列表
     */
    public function list(Request $request)
    {
        try {
            $merchant_id = $request->merchant_id;
            
            $list = MerchantPaymentConfig::where('merchant_id', $merchant_id)
                ->order('id', 'desc')
                ->select();
                
            return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
        } catch (\Exception $e) {
            Log::error('获取支付配置列表失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 获取支付配置详情
     */
    public function detail(Request $request)
    {
        try {
            $merchant_id = $request->merchant_id;
            $payment_method = $request->get('payment_method');
            
            if (!$payment_method) {
                return json(['code' => 1, 'msg' => '参数错误']);
            }
            
            $config = MerchantPaymentConfig::where([
                'merchant_id' => $merchant_id,
                'payment_method' => $payment_method
            ])->find();
            
            return json(['code' => 0, 'msg' => '获取成功', 'data' => $config]);
        } catch (\Exception $e) {
            Log::error('获取支付配置详情失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 更新支付配置
     */
    public function update(Request $request)
    {
        try {
            $params = $request->post();
            $merchant_id = $request->merchant_id;
            
            // 验证参数
            $validate = validate('MerchantPaymentConfig');
            if (!$validate->check($params)) {
                return json(['code' => 1, 'msg' => $validate->getError()]);
            }
            
            // 检查配置是否已存在
            $config = MerchantPaymentConfig::where([
                'merchant_id' => $merchant_id,
                'payment_method' => $params['payment_method']
            ])->find();
            
            if ($config) {
                // 更新
                $config->save([
                    'payment_config' => json_encode($params['payment_config'], JSON_UNESCAPED_UNICODE),
                    'update_time' => date('Y-m-d H:i:s')
                ]);
            } else {
                // 新增
                MerchantPaymentConfig::create([
                    'merchant_id' => $merchant_id,
                    'payment_method' => $params['payment_method'],
                    'payment_config' => json_encode($params['payment_config'], JSON_UNESCAPED_UNICODE),
                    'status' => 1
                ]);
            }
            
            return json(['code' => 0, 'msg' => '更新成功']);
        } catch (\Exception $e) {
            Log::error('更新支付配置失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 设置支付配置状态
     */
    public function setStatus(Request $request)
    {
        try {
            $params = $request->post();
            $merchant_id = $request->merchant_id;
            
            if (!isset($params['payment_method']) || !isset($params['status'])) {
                return json(['code' => 1, 'msg' => '参数错误']);
            }
            
            MerchantPaymentConfig::where([
                'merchant_id' => $merchant_id,
                'payment_method' => $params['payment_method']
            ])->update([
                'status' => $params['status'],
                'update_time' => date('Y-m-d H:i:s')
            ]);
            
            return json(['code' => 0, 'msg' => '设置成功']);
        } catch (\Exception $e) {
            Log::error('设置支付配置状态失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }
} 